(*
     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is CrossFPC.

     The Initial Developer of the Original Code is Simon Kissel.
     Portions created by Benjamin Rosseaux are Copyright (C) Benjamin Rosseaux.
     All Rights Reserved.

*)

unit cfcompileroptionsdialog;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cfCompilerOptions, StdCtrls, ComCtrls, ExtCtrls;

{
  Note: The checkboxes have their tag set to ORD(TEnumType)
  do not change this, or the options will not be saved/loaded
}

type
  TCompilerOptionsForm = class(TForm)
    PCoptions: TPageControl;
    TSDirectories: TTabSheet;
    TabSheet2: TTabSheet;
    EOutputDir: TEdit;
    EUnitOutputDir: TEdit;
    EUnitSearchPath: TEdit;
    EExecutableDir: TEdit;
    EUtilsDir: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    EDynamicLinker: TEdit;
    EIncludePath: TEdit;
    EObjectPath: TEdit;
    ELinkerPath: TEdit;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    TSVerbosity: TTabSheet;
    GBCode: TGroupBox;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox6: TCheckBox;
    CheckBox7: TCheckBox;
    CheckBox8: TCheckBox;
    CheckBox9: TCheckBox;
    CheckBox10: TCheckBox;
    GBSyntax: TGroupBox;
    CheckBox11: TCheckBox;
    CheckBox12: TCheckBox;
    CheckBox13: TCheckBox;
    CheckBox14: TCheckBox;
    CheckBox15: TCheckBox;
    CheckBox16: TCheckBox;
    CheckBox17: TCheckBox;
    CheckBox18: TCheckBox;
    CheckBox19: TCheckBox;
    CheckBox20: TCheckBox;
    BDir: TButton;
    Label12: TLabel;
    CBOs: TComboBox;
    GBVerbose: TGroupBox;
    CheckBox21: TCheckBox;
    CheckBox22: TCheckBox;
    CheckBox23: TCheckBox;
    CheckBox24: TCheckBox;
    CheckBox25: TCheckBox;
    CheckBox26: TCheckBox;
    CheckBox27: TCheckBox;
    CheckBox28: TCheckBox;
    CheckBox29: TCheckBox;
    CheckBox30: TCheckBox;
    CheckBox31: TCheckBox;
    CheckBox32: TCheckBox;
    CheckBox33: TCheckBox;
    CheckBox34: TCheckBox;
    CheckBox35: TCheckBox;
    GBInfo: TGroupBox;
    CheckBox36: TCheckBox;
    CheckBox37: TCheckBox;
    CheckBox38: TCheckBox;
    CheckBox39: TCheckBox;
    CheckBox40: TCheckBox;
    CheckBox41: TCheckBox;
    Label4: TLabel;
    EMessageFile: TEdit;
    EErrorOutput: TEdit;
    Label13: TLabel;
    CBLogo: TCheckBox;
    CBHelp: TCheckBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    Button11: TButton;
    TSAssembler: TTabSheet;
    GBAsmOutput: TGroupBox;
    GBAsmInput: TGroupBox;
    Label14: TLabel;
    CBAsmReader: TComboBox;
    Label15: TLabel;
    CBAsmWriter: TComboBox;
    CheckBox42: TCheckBox;
    CheckBox43: TCheckBox;
    CheckBox44: TCheckBox;
    CheckBox45: TCheckBox;
    CheckBox46: TCheckBox;
    CheckBox47: TCheckBox;
    Label16: TLabel;
    Label17: TLabel;
    ELinkerOptions: TEdit;
    GBExecutable: TGroupBox;
    CheckBox48: TCheckBox;
    CheckBox49: TCheckBox;
    CheckBox50: TCheckBox;
    CheckBox51: TCheckBox;
    CheckBox52: TCheckBox;
    CheckBox53: TCheckBox;
    CheckBox54: TCheckBox;
    GBScript: TGroupBox;
    CheckBox55: TCheckBox;
    CheckBox56: TCheckBox;
    CheckBox57: TCheckBox;
    CheckBox58: TCheckBox;
    CBCallingConvention: TComboBox;
    CBMode: TComboBox;
    Label18: TLabel;
    Label19: TLabel;
    EStackSize: TEdit;
    EHeapSize: TEdit;
    LStackSize: TLabel;
    LHeapSize: TLabel;
    TabSheet1: TTabSheet;
    GBDebug: TGroupBox;
    GBoptimizations: TGroupBox;
    CheckBox59: TCheckBox;
    CheckBox60: TCheckBox;
    CheckBox61: TCheckBox;
    CheckBox62: TCheckBox;
    CheckBox63: TCheckBox;
    CheckBox64: TCheckBox;
    CheckBox65: TCheckBox;
    CheckBox66: TCheckBox;
    CBProfiling: TCheckBox;
    ECrossLinkPath: TEdit;
    LCrossLinkPath: TLabel;
    Button12: TButton;
    ECrossLinkLibPrefix: TEdit;
    LCrossToolPrefix: TLabel;
    PBottom: TPanel;
    BOK: TButton;
    BCancel: TButton;
    Label8: TLabel;
    EOutputFileName: TEdit;
    Button4: TButton;
    CheckBox67: TCheckBox;
    CheckBox68: TCheckBox;
    CheckBox69: TCheckBox;
    CheckBox70: TCheckBox;
    CheckBox71: TCheckBox;
    CheckBox72: TCheckBox;
    CheckBox73: TCheckBox;
    CheckBox74: TCheckBox;
    CheckBox75: TCheckBox;
    CheckBox76: TCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure BOKClick(Sender: TObject);
  private
    Foptions: TCompilerOptions;
    { Private declarations }
    procedure Form2Options(C: TCompilerOptions);
    procedure Options2Form(C: TCompileroptions);
    procedure SetOptions(Value: TCompilerOptions);
  public
    { Public declarations }
    property Options: TCompilerOptions read Foptions write SetOptions;
  end;

var
  CompilerOptionsForm: TCompilerOptionsForm;

implementation

{$R *.dfm}

{ TCompilerOptionsForm }

procedure TCompilerOptionsForm.Options2Form(C: TCompileroptions);

var
  I: Integer;

begin
  with C do
  begin
    // Directories tab
    EOutputDir.Text := OutputDir;
    EUnitOutputDir.Text := UnitOutputDir;
    EOutputFileName.Text := OutputFileName;
    EUnitSearchPath.Text := UnitSearchPath;
    EExecutableDir.Text := ExeDirectory;
    EUtilsDir.Text := UtilityDirectory;
    EIncludePath.Text := IncludeSearchPath;
    ELinkerPath.Text := LibrarySearchPath;
    EObjectPath.Text := ObjectSearchPath;
    EDynamicLinker.text := DynamicLinker;
    ECrossLinkPath.Text := CrossLinkLibraryPath;
    // Code & Syntax tab
    CBOS.ItemIndex := Ord(TargetOS) - 1;
    with GBCode do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            Checked := TCodeOption(Tag) in CodeOptions;
    with GBSyntax do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            Checked := TSyntaxOption(Tag) in SyntaxOptions;
    CBCallingConvention.ItemIndex := Ord(CallingCOnvention) - 1;
    CBMode.ItemIndex := Ord(CompilerMode) - 1;
    // verbosity tab
    with GBVerbose do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            Checked := TVerbosity(Tag) in Verbosity;
    with GBInfo do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            Checked := TInfoOption(Tag) in ShowInformation;
    CBLogo.Checked := ShowLogo;
    CBHelp.Checked := ShowHelp;
    EMessageFile.Text := MessageFile;
    EErrorOutput.Text := MessageOutputFile;
    { Assembler & Linker}
    CBAsmReader.ItemIndex := Ord(AssemblerStyle) - 1;
    CBAsmWriter.ItemIndex := Ord(OutputFormat) - 1;
    with GBAsmOutput do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            Checked := TAssemblerOption(Tag) in AssemblerOptions;
    with GBExecutable do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            Checked := TExecutableOption(Tag) in ExecutableOptions;
    with GBScript do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            Checked := TScriptOption(Tag) in ScriptOptions;
    ELinkerOptions.text := LinkerOptions;
    ECrossLinkLibPrefix.Text := CrossLinkLibPrefix;
    { Extra }
    with GBDebug do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            Checked := TDebugOption(Tag) in DebugOptions;
    with GBOptimizations do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            Checked := TOPtimization(Tag) in Optimizations;
    CBProfiling.Checked := ProfilingInfo;
    {
      Not used in CrossFPC
        Property CrossLinkLibraryPath : String Read FCrossLinkLibraryPath Write FCrossLinkLibraryPath; // -Xr
        Property CrossLinkLibPrefix : String Read FCrosslinkLibPrefix Write FCrossLinkLibPRefix; // -XP
        Property Defines           : String Read FDefines Write FDefines;                   // -D
        Property UnDefines         : String Read FUnDefines Write FUnDefines;               // -U
        Property InputCodePage     : String Read FCodePage Write FCodePage;                 // -Fc
        Property BrowserOptions    : TBrowserOptions Read FBrowser Write FBrowser;          // -b
        Property Build             : Boolean Index bBuild Read GetBool write SetBool;       // -B
        Property FPUInstructionSet : TFPUSet Read FPU Write FPU;                            // -CF
        Property SkipDefaultConfig : Boolean Index bSkipDefaultConfig Read GetBool Write SetBool; // -n
        Property UnitOptions       : TUnitOptions Read FUnitOptions Write FUnitOptions;       // -U
        // Win32/OS2 stuff
        Property GenerateDefFile   : Boolean Index bGenerateDefFile Read GetBool Write SetBool;  // -D
        Property DefDescription    : String Read FDefDescription Write FDefDescription;     // -Dd
        Property DefVersion        : String Read FDefVersion Write FDefVersion;             // -Dv
        Property Win32Options      : TWin32Options Read FWin32Options Write FWin32Options;    // -W
        Property Win32ImageBase    : Integer Read FImageBase Write FImageBase;                // -WB
    }
  end;
end;

procedure TCompilerOptionsForm.Form2Options(C: TCompilerOptions);

var
  I: Integer;
  CO: TCodeOptions;
  SO: TSyntaxOptions;
  VO: TVerbosityOPtions;
  SI: TInfoOptions;
  AO: TAssemblerOptions;
  EO: TExecutableOptions;
  SCO: TScriptOptions;
  DBO: TDebugOptions;
  Oo: TOptimizations;

begin
  with C do
  begin
    // Directories tab
    OutputDir := EOutputDir.Text;
    UnitOutputDir := EUnitOutputDir.Text;
    OutputFileName := EOutputFileName.Text;
    UnitSearchPath := EUnitSearchPath.Text;
    ExeDirectory := EExecutableDir.Text;
    UtilityDirectory := EUtilsDir.Text;
    IncludeSearchPath := EIncludePath.Text;
    LibrarySearchPath := ELinkerPath.Text;
    ObjectSearchPath := EObjectPath.Text;
    DynamicLinker := EDynamicLinker.text;
    ECrossLinkPath.Text := CrossLinkLibraryPath;
    // Code & Syntax tab
    TargetOS := TTargetOS(CBOS.ItemIndex + 1);
    CO := [];
    with GBCode do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            if Checked then
              Include(CO, TCodeOption(Tag));
    CodeOptions := CO;
    SO := [];
    with GBSyntax do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            if Checked then
              Include(SO, TSyntaxOption(Tag));
    SyntaxOptions := SO;
    CompilerMode := TCompilerMode(CBMode.ItemIndex + 1);
    CallingCOnvention := TCallingConvention(CBCallingConvention.ItemIndex + 1);
    // verbosity tab
    VO := [];
    with GBVerbose do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            if Checked then
              Include(VO, TVerbosity(Tag));
    Verbosity := VO;
    SI := [];
    with GBInfo do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            if Checked then
              Include(SI, TInfoOption(Tag));
    ShowInformation := SI;
    ;
    ShowLogo := CBLogo.Checked;
    ShowHelp := CBHelp.Checked;
    MessageFile := EMessageFile.Text;
    MessageOutputFile := EErrorOutput.Text;
    { Assembler & Linker}
    AssemblerStyle := TAssemblerStyle(CBAsmReader.ItemIndex + 1);
    OutputFormat := TOutputFormat(CBAsmWriter.ItemIndex + 1);
    AO := [];
    with GBAsmOutput do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            if Checked then
              Include(AO, TAssemblerOption(Tag));
    AssemblerOptions := AO;
    EO:=[];
    with GBExecutable do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            if Checked then
              Include(EO, TExecutableOption(Tag));
    ExecutableOptions:=EO;        
    SCO := [];
    with GBScript do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            if Checked then
              Include(SCO, TScriptOption(Tag));
    ScriptOptions := SCO;
    LinkerOptions := ELinkerOptions.text;
    CrossLinkLibPrefix := ECrossLinkLibPrefix.Text;
    { Extra }
    DBO := [];
    with GBDebug do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            if Checked then
              Include(DBO, TDebugOption(Tag));
    DebugOptions := DBO;
    OO := [];
    with GBOptimizations do
      for I := 0 to ControlCount - 1 do
        if (Controls[i] is TCheckBox) then
          with TCheckBox(Controls[i]) do
            if Checked then
              Include(OO, TOPtimization(Tag));
    Optimizations := OO;
    ProfilingInfo := CBProfiling.Checked;
  end;
end;

procedure TCompilerOptionsForm.FormCreate(Sender: TObject);

var
  OT: TTargetOS;
  AR: TAssemblerStyle;
  AW: TOutputFormat;
  CC: TCallingConvention;
  CM: TCompilerMode;

begin
  with CBOS do
    for OT := Succ(Low(TTargetOS)) to High(TTargetOS) do
      Items.Add(STargetOS[OT]);
  with CBAsmReader do
    for AR := Succ(Low(TAssemblerStyle)) to High(TAssemblerStyle) do
      Items.Add(SAssemblerStyle[AR]);
  with CBAsmWriter do
    for AW := Succ(Low(TOutputFormat)) to High(TOutputFormat) do
      Items.Add(SOutputFormat[AW]);
  with CBCallingConvention do
    for CC := Succ(Low(TCallingConvention)) to High(TCallingConvention) do
      Items.Add(SCallingConvention[CC]);
  with CBMode do
    for CM := Succ(Low(TCompilerMode)) to High(TCompilerMode) do
      Items.Add(SCompilerMode[CM]);
end;

procedure TCompilerOptionsForm.SetOptions(Value: TCompilerOptions);
begin
  Foptions := Value;
  Options2Form(FOptions);
end;

procedure TCompilerOptionsForm.BOKClick(Sender: TObject);
begin
  Form2Options(FOptions);
end;

end.

